package com.dcliment.controllers
{
	import com.dcliment.models.AttractLoopModel;
	import com.dcliment.utils.CustomTimer;
	import com.dcliment.utils.DisplayObjectUtil;
	import com.dcliment.video.VideoPlayer;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	/**
	 * Looping Attract Loop
	 * 
	 * @author dcliment
	 * version 1.2
	 * 
	 * Note :: adding having static content 
	 */	
	
	public class AttractLoopController extends EventDispatcher implements IDisplayController
	{
		private var _model:AttractLoopModel;
		private var _view:Sprite;
		//
		private var _video1:VideoPlayer;
		private var _video2:VideoPlayer;
		//
		private var _activeVideo:VideoPlayer;
		private var _active:Boolean; // if attract loop is active
		//
		private var _checkTimer:CustomTimer;
		
		/**
		 * Example:
		 * var _attractLoopModel:AttractLoopModel 	= new AttractLoopModel();
		 *	_attractLoopModel.src 					= URL
		 *	_attractLoopModel.width 				= VDIEO WIDTH // if null it will take the width of the video itself
		 *	_attractLoopModel.height 				= VDIEO HEIGHT // if null it will take the width of the video itself
		 *  _attractLoopModel.posX					= 0
		 *  _attractLoopModel.posY					= 0
		 * 
		 * var _attractLoopController:AttractLoopController 	= new AttractLoopController(_container Sprite);
		 * _attractLoopController.init(_attractLoopModel);
		 * 
		 * @param $view :: Where do you want it
		 * 
		 */
		
		public function AttractLoopController($view:Sprite)
		{
			_view = $view;
			
			_checkTimer = new CustomTimer(500, checkVideoIsPlaying, 1);
		}
		public function init($model:*):void
		{
			
			_model = $model as AttractLoopModel;
			
			if(!_model)
				throw new Error("AttractLoopController.init ERROR :: _model passed was not the AttractLoopModel"); 
			
			//----------------------
			// 
			if(_model.contentName)
			{
				var content:MovieClip = DisplayObjectUtil.getLibraryObject(_model.contentName);
					content.x = _model.contentX;
					content.y = _model.contentY;
				
				_view.addChild(content);
			}
			
			
			//-----------------------
			// VIDEOS
			_video1 = videoFactory();
			_view.addChild(_video1);
			
			_video2 = videoFactory();
			_video2.pause();
			_video2.seek(0);
			
			
			_activeVideo = _video1;
			// this makes sure that all settings are active
			_active = true;
			
		}
		private function swapVideo(e:Event):void
		{
			//trace("swapVideo fired");
			
			if(_activeVideo == _video1)
			{
				_activeVideo = _video2;
				_video1.seek(0);
			}
			else
			{
				_activeVideo = _video1;
				_video2.seek(0);
			}
			
			_activeVideo.resume();
			_view.addChild(_activeVideo);
			
			// FAIL SAFE FOR VIDEO
			_checkTimer.reset();
			_checkTimer.start();
		}
		private function videoFactory():VideoPlayer
		{
			var video:VideoPlayer = new VideoPlayer();
				video.createPlayer(_model.src, _model.width, _model.height, true);
				video.x = _model.posX;
				video.y = _model.posY;
				video.init();
				video.addEventListener(Event.COMPLETE, swapVideo, false, 0, true);
				
			return video;
				
		}
		// ------------ PUBLIC FUNCTIONS ---------------
		/**
		 * HIDE and Stop playing Attract Loop 
		 * 
		 */		
		public function hide():void
		{
			_activeVideo.pause();
			_view.visible = false;
			
			_activeVideo.seek(0);
			
			_active = false;
			
		}
		/**
		 * Activate and show Attract Loop 
		 * 
		 */		
		public function show():void
		{
			_activeVideo.resume();
			
			_view.visible = true;
			
			_active = true;
		}
		public function pause():void
		{
			_activeVideo.pause();
		}
		public function resume():void
		{
			_activeVideo.resume();
		}
		//------------------------------------
		// Event Listener (check and balance);
		private function checkVideoIsPlaying():void
		{
			trace("video playhead = " + _activeVideo.playheadTime);
			
			if(_activeVideo.playheadTime > 0)
			{	
				
				//_checkTimer.start();
				//_checkTimer.stop();
				//_checkTimer.reset();
				trace("AttractLoopController.checkVideoisPlaying() :: !!!!!! VIDEO is playing !!!!!!!");
			}
			else
			{
				_activeVideo.play();
				_checkTimer.start();
				trace("AttractLoopController.checkVideoIsPlaying() :: ??????? VIDEO is NOT playing ???????");
			}
		}
		//------------------------------------
		// ACCESSORS
		public function get active():Boolean 
		{
			return _active;
		}

	}
}